#pragma once
#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&_lock, nullptr);
    }

    void lock()
    {
        pthread_mutex_lock(&_lock);
        // std::cout<<"上锁成功"<<std::endl;
    }

    void unlock()
    {
        pthread_mutex_unlock(&_lock);
        // std::cout<<"解锁成功"<<std::endl;
    }
    pthread_mutex_t *Get()
    {
        return &_lock;
    }
    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }

private:
    pthread_mutex_t _lock;
};

class LockGuarden
{
public:
    LockGuarden(Mutex *mutex)
        : _mutex(mutex)
    {
        _mutex->lock();
    }
    ~LockGuarden()
    {
        _mutex->unlock();
    }

private:
    Mutex *_mutex;
};